import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import { ApNetService } from './ap_net.service';

@Controller('ap/net')
export class ApNetController {
  constructor(private readonly apNetService: ApNetService) {}
  public lanInfo = {
    ip: '192.168.1.1',
  };

  public caaGreInfo = {
    caa_ip: '192.168.100.2',
    caa_status: 'off',
    gre_status: 'on',
  };

  @Post('/lan/lan_get')
  lan_get() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        ip: this.lanInfo.ip,
      },
    };
  }

  @Post('/lan/lan_set')
  async lan_set(@Body('ip') body: any) {
    this.lanInfo.ip = body;
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return {
      code: 200,
      message: '设置成功',
      data: {
        ip: this.lanInfo.ip,
      },
    };
  }

  //获取CAA与GRE的连接状态
  @Post('/caa_gre_get')
  caa_gre_get() {
    return {
      code: 200,
      message: '获取成功',
      data: this.caaGreInfo,
    };
  }

  //设置CAA与GRE的连接状态
  @Post('/caa_gre_set')
  caa_gre_set(@Body() body: any) {
    this.caaGreInfo = body;
    return {
      code: 200,
      message: '设置成功',
      data: this.caaGreInfo,
    };
  }

  public reverseNatInfo = {
    ip: '192.168.1.1',
    status: 'on',
  };

  //反向nat信息获取
  @Post('/lan/reverse_nat_get')
  reverse_nat_get() {
    return {
      code: 200,
      message: '获取成功',
      data: this.reverseNatInfo,
    };
  }

  //反向nat信息设置
  @Post('/lan/reverse_nat_set')
  reverse_nat_set(@Body() body: any) {
    this.reverseNatInfo = body;
    return {
      code: 200,
      message: '设置成功',
      data: this.reverseNatInfo,
    };
  }

  //WIFI信息获取
  public wifiInfo = [
    {
      frequency_band: '2.4g',
      ssid: 'CSCN_WIFI_2.4G',
      password: '12345678',
      status: 'on',
      id: 1,
    },
    {
      frequency_band: '2.4g',
      ssid: 'CSCN_WIFI_2.4G',
      password: '12345678',
      status: 'on',
      id: 2,
    },
  ];
  @Post('/lan/wifi_get')
  wifi_get() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: this.wifiInfo,
      },
    };
  }

  //WIFI信息设置
  @Post('/lan/wifi_set')
  wifi_set(@Body() body: any) {
    this.wifiInfo = this.wifiInfo.map((item: any) =>
      item.id === body.id ? body : item,
    );
    return {
      code: 200,
      message: '设置成功',
      data: this.wifiInfo,
    };
  }

  //DHCP信息获取
  public dhcpInfo = {
    start_ip: '192.168.10.100',
    end_ip: '192.168.10.254',
    DNS1: '8.8.8.8',
    DNS2: '8.8.8.8',
    status: 'on',
  };
  @Post('/lan/dhcp_get')
  dhcp_get() {
    return {
      code: 200,
      message: '获取成功',
      data: this.dhcpInfo,
    };
  }

  //DHCP信息设置
  @Post('/lan/dhcp_set')
  dhcp_set(@Body() body: any) {
    this.dhcpInfo = body;
    return {
      code: 200,
      message: '设置成功',
      data: this.dhcpInfo,
    };
  }

  //IP加速信息获取
  public ipAccelerationInfo = {
    status: 'on',
  };
  @Post('/ip_accelerate_get')
  ip_acceleration_get() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        status: this.ipAccelerationInfo.status,
      },
    };
  }

  //IP加速信息设置
  @Post('/ip_accelerate_set')
  async ip_acceleration_set(@Body() body: any) {
    this.ipAccelerationInfo = body;
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return {
      code: 200,
      message: '设置成功',
      data: {
        status: this.ipAccelerationInfo.status,
      },
    };
  }

  //ALOHA信息获取,枚举值,0禁止,1手动,2自动
  public alohaInfo = {
    status: 1,
  };
  @Post('/aloha_get')
  aloha_get() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        status: this.alohaInfo.status,
      },
    };
  }

  //ALOHA信息设置
  @Post('/aloha_set')
  aloha_set(@Body() body: any) {
    this.alohaInfo = body;
    return {
      code: 200,
      message: '设置成功',
      data: {
        status: this.alohaInfo.status,
      },
    };
  }

  //DNN信息获取
  public dnnInfo: any = [
    {
      id: 2,
      name: 'private_net_dnn1',
      type: 'private_net',
      portmap: '3',
      dnn_ip: '192.168.10.110',
      dhcp_relay: 'on',
      dhcp_start_ip: '192.168.10.100',
      dhcp_end_ip: '192.168.10.254',
      dhcp_dns1: '8.8.8.8',
      dhcp_dns2: '114.114.114.114',
      dhcp_gw: '192.168.10.1',
      dhcp_mask: '255.255.255.0',
      dnn_status: 0,
      connect: 'off',
      lan_ip: '192.168.10.1',
      vlan_id: 10,
      nat_status: 'off',
      local_status: 'off',
      using_dnn: 'private_net_dnn1',
      dhcp_status: 'off',
    },

    {
      id: 1,
      name: 'language_dnn2',
      type: 'language',
      portmap: '7',
      dnn_ip: '192.168.10.110',
      dhcp_relay: 'on',
      dhcp_start_ip: '192.168.10.100',
      dhcp_end_ip: '192.168.10.254',
      dhcp_dns1: '8.8.8.8',
      dhcp_dns2: '114.114.114.114',
      dhcp_gw: '192.168.10.1',
      dhcp_mask: '255.255.255.0',
      dnn_status: 0,
      connect: 'on',
      lan_ip: '192.168.10.1',
      vlan_id: 10,
      nat_status: 'on',
      local_status: 'on',
      using_dnn: 'language_dnn2',
      dhcp_status: 'off',
    },
    {
      id: 4,
      name: 'xwtj_dnn4',
      type: 'xwtj',
      portmap: '5',
      dnn_ip: '192.168.10.110',
      dhcp_relay: 'on',
      dhcp_start_ip: '192.168.10.100',
      dhcp_end_ip: '192.168.10.254',
      dhcp_dns1: '8.8.8.8',
      dhcp_dns2: '114.114.114.114',
      dhcp_gw: '192.168.10.1',
      dhcp_mask: '255.255.255.0',
      dnn_status: 0,
      connect: 'off',
      lan_ip: '192.168.10.1',
      vlan_id: 10,
      nat_status: 'on',
      local_status: 'off',
      using_dnn: 'xwtj_dnn4',
      dhcp_status: 'on',
    },
    {
      id: 3,
      name: 'internet_dnn3',
      type: 'internet',
      portmap: '8',
      dnn_ip: '192.168.10.110',
      dhcp_relay: 'on',
      dhcp_start_ip: '192.168.10.100',
      dhcp_end_ip: '192.168.10.254',
      dhcp_dns1: '8.8.8.8',
      dhcp_dns2: '114.114.114.114',
      dhcp_gw: '192.168.10.1',
      dhcp_mask: '255.255.255.0',
      dnn_status: 0,
      connect: 'on',
      lan_ip: '192.168.10.1',
      vlan_id: 10,
      nat_status: 'on',
      local_status: 'off',
      using_dnn: 'internet_dnn3',
      dhcp_status: 'off',
    },
    {
      id: 5,
      name: 'caa_dnn5',
      type: 'caa',
      portmap: '3',
      dnn_ip: '192.168.10.110',
      dhcp_relay: 'on',
      dhcp_start_ip: '192.168.10.100',
      dhcp_end_ip: '192.168.10.254',
      dhcp_dns1: '8.8.8.8',
      dhcp_dns2: '114.114.114.114',
      dhcp_gw: '192.168.10.1',
      dhcp_mask: '255.255.255.0',
      dnn_status: 0,
      connect: 'on',
      lan_ip: '192.168.10.1',
      vlan_id: 10,
      nat_status: 'off',
      local_status: 'off',
      using_dnn: 'caa_dnn5',
      dhcp_status: 'on',
    },
  ];
  @Post('/dnn_get')
  dnn_get() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: this.dnnInfo,
      },
    };
  }

  //DNN信息设置
  @Post('/dnn_set')
  dnn_set(@Body() body: any) {
    if (body.id) {
      this.dnnInfo = this.dnnInfo.map((item: any) =>
        item.id === body.id ? body : item,
      );
    } else {
      this.dnnInfo.push(body);
      this.speed_list.push({
        name: body.name,
        tx_bytes_ps: 0,
        rx_bytes_ps: 0,
        tx_pkt_ps: '',
        rx_pkt_ps: '',
        update_timestamp: 0,
      });
    }
    return {
      code: 200,
      message: '设置成功',
      data: this.dnnInfo,
    };
  }

  //删除DNN
  @Post('/dnn_del')
  dnn_delete(@Body() body: any) {
    this.dnnInfo = this.dnnInfo.filter((item: any) => item.id !== body.id);
    return {
      code: 200,
      message: '删除成功',
      data: this.dnnInfo,
    };
  }
  public iperf3Result = {
    client_status: '111111111111',
    server_status: '222222222222',
  };

  public iperf3_status = '';

  //测试IPERF3客户端
  @Post('/tool/iperf3_client')
  async iperf3_client(@Body() body: any) {
    this.iperf3Result.client_status = '111111111111';
    this.iperf3Result.server_status = '222222222222';
    setTimeout(() => {
      this.iperf3Result = {
        client_status: '',
        server_status: '',
      };
    }, 10000);
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return {
      code: 200,
      message: '测试成功',
      data: {
        iperf_status:
          this.iperf3_status === 'server started'
            ? 'client started'
            : 'client not started',
      },
    };
  }

  //测试IPERF3服务端
  @Post('/tool/iperf3_server')
  iperf3_server(@Body() body: any) {
    this.iperf3Result.client_status = '111111111111';
    this.iperf3Result.server_status = '222222222222';
    this.iperf3_status = 'server started';
    setTimeout(() => {
      this.iperf3Result = {
        client_status: '',
        server_status: '',
      };
    }, 10000);
    return {
      code: 200,
      message: '测试成功',
      data: {
        iperf_status: 'server started',
      },
    };
  }

  @Post('/tool/iperf3_get')
  iperf3_get(@Body() body: any) {
    return {
      code: 200,
      message: '获取成功',
      data: this.iperf3Result,
    };
  }

  @Post('/tool/iperf3_stop')
  iperf3_stop(@Body() body: any) {
    this.iperf3Result = {
      client_status: 'client not started',
      server_status: 'server not started',
    };
    return {
      code: 200,
      message: '停止成功',
      data: this.iperf3Result,
    };
  }

  private airplaneModel = 'off';

  @Post('airplane_mode_get')
  getAirplaneMode() {
    return {
      code: 200,
      data: {
        status: this.airplaneModel,
      },
    };
  }

  @Post('airplane_mode_set')
  setAirplaneMode(@Body() body: any) {
    this.airplaneModel = body.status;
    return {
      code: 200,
      data: {
        status: this.airplaneModel,
      },
    };
  }

  public speed_list = [
    {
      name: 'private_net_dnn1',
      tx_bytes_ps: Math.floor(Math.random() * 10000),
      rx_bytes_ps: Math.floor(Math.random() * 10000),
      tx_pkt_ps: '',
      rx_pkt_ps: '',
      update_timestamp: 0,
    },
    {
      name: 'language_dnn2',
      tx_bytes_ps: Math.floor(Math.random() * 10000),
      rx_bytes_ps: Math.floor(Math.random() * 10000),
      tx_pkt_ps: '',
      rx_pkt_ps: '',
      update_timestamp: 0,
    },
    {
      name: 'internet_dnn3',
      tx_bytes_ps: Math.floor(Math.random() * 10000),
      rx_bytes_ps: Math.floor(Math.random() * 10000),
      tx_pkt_ps: '',
      rx_pkt_ps: '',
      update_timestamp: 0,
    },
    {
      name: 'xwtj_dnn4',
      tx_bytes_ps: Math.floor(Math.random() * 10000),
      rx_bytes_ps: Math.floor(Math.random() * 10000),
      tx_pkt_ps: '',
      rx_pkt_ps: '',
      update_timestamp: 0,
    },
    {
      name: 'caa_dnn5',
      tx_bytes_ps: Math.floor(Math.random() * 10000),
      rx_bytes_ps: Math.floor(Math.random() * 10000),
      tx_pkt_ps: '',
      rx_pkt_ps: '',
    },
  ];

  setSpeedList() {
    this.speed_list.forEach((item) => {
      item.tx_bytes_ps = Math.floor(Math.random() * 10000);
      item.rx_bytes_ps = Math.floor(Math.random() * 10000);
    });
    return this.speed_list;
  }

  // dnn网络速率获取
  @Post('/host_speed_get')
  dnn_speed_get(@Body('dnn_name') dnn_name: any) {
    return {
      code: 200,
      data: {
        speed_list: this.setSpeedList(),
      },
    };
  }

  // dnn配置获取
  public apnInfos: any = [
    {
      name: 'xwtj_dnn',
      using_apn: 'string',
      apn_enable: 1,
    },
    {
      name: 'internet_dnn1',
      using_apn: 'string',
      apn_enable: 1,
    },
  ];
  @Post('/apn_get')
  apn_get() {
    return {
      code: 200,
      data: {
        apn_get: this.apnInfos,
      },
    };
  }

  // dnn配置设置
  @Post('/apn_set')
  apn_set(@Body('apn_set') apn_set: any) {
    this.apnInfos = apn_set;
    return {
      code: 200,
      data: this.apnInfos,
    };
  }

  //dnn列表获取
  public apnList = [
    {
      apn: 'string1',
      id: 1,
    },
    {
      apn: 'string2',
      id: 2,
    },
  ];
  @Post('/apn_list_get')
  dnn_list_get() {
    return {
      code: 200,
      data: {
        list_get: this.apnList,
      },
    };
  }

  //增加APN
  @Post('/apn_list_add')
  apn_add(@Body() body: any) {
    this.apnList.push(body);
    return {
      code: 200,
      data: this.apnList,
    };
  }

  //删除APN
  @Post('/apn_list_del')
  apn_del(@Body() body: any) {
    this.apnList = this.apnList.filter((item: any) => item.apn !== body.apn);
    return {
      code: 200,
      data: this.apnList,
    };
  }

  //PPPoE信息获取

}
